<template>
  <div style="padding-bottom: 60px">
    <van-swipe class="my-swipe" indicator-color="skyblue">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" width="100%" style="display: block;" alt />
      </van-swipe-item>
    </van-swipe>
    <wan-shan></wan-shan>
    <div class="product_info">
      <h3>{{info.name}}</h3>
      <div>{{info.goods_brief}}</div>
      <p>{{Number(info.retail_price) | filterMoney}}</p>
    </div>
    <van-cell title="展示弹出层" is-link @click="ifShowSku = !ifShowSku" />
    <div class="canshu">
      <h3>商品参数</h3>
      <ul>
        <li v-for="(item, index) in attribute" :key="index">
          <small>{{item.name}}</small>
          <big class="van-ellipsis">{{item.value}}</big>
        </li>
      </ul>
    </div>
    <div class="box" ref="box"></div>
    <div class="issue">
      <div class="mytitle">
        <span></span>
        <h3>常见问题</h3>
      </div>
      <ul>
        <li v-for="item in issue" :key="item.id">
          <h3>
            <i></i>
            {{item.question}}
          </h3>
          <div>{{item.answer}}</div>
        </li>
      </ul>
    </div>
    <div class="allLook">
      <div class="mytitle">
        <span></span>
        <h3>大家都在看</h3>
      </div>
      <ProductList :goodsList="goodsList" />
    </div>
    <van-goods-action>
      <van-goods-action-icon
        :icon="cartLanObj.ifSave ? 'star' : 'star-o'"
        :text="cartLanObj.ifSave ? '已收藏' : '未收藏'"
        @click="cartLanObj.ifSave = !cartLanObj.ifSave"
        :color="cartLanObj.ifSave ? 'darkred' : '#666'"
      />
      <van-goods-action-icon icon="cart-o" text="购物车" :badge="cartLanObj.badge" @click="goToCart" />
      <van-goods-action-button type="danger" text="立即购买" @click="buyNow" />
      <van-goods-action-button type="warning" text="加入购物车" @click="addToCartFn" />
    </van-goods-action>
    <transition name="van-fade">
      <div class="modal" v-if="ifShowSku">
        <div class="modal_bg" @click="ifShowSku = false"></div>
        <div class="modal_content">
          <van-icon name="cross" class="close" size="20" @click="ifShowSku = false" />
          <div class="top">
            <img :src="info.list_pic_url" width="100" height="100" alt />
            <div class="right">
              <section>价格：{{info.retail_price | filterMoney}}</section>
              <section>库存：{{info.goods_number}}</section>
            </div>
          </div>
          <div class="down">
            <h3>数量：</h3>
            <van-stepper v-model="stepValue" min="1" :max="info.goods_number" />
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import ProductList from "@/components/ProductList";
import WanShan from "@/components/WanShan";
import {
  GetProductDetailAPI,
  GoodsCountAPI,
  AddToCartAPI,
  GetAboutAPI,
} from "@/request/api";
export default {
  data() {
    return {
      // 轮播图
      gallery: [],
      // 商品信息
      info: {},
      // 商品参数
      attribute: [],
      // 步进器
      stepValue: 1,
      // sku的显示隐藏
      ifShowSku: false,
      // 购物栏
      cartLanObj: {
        // 收藏,true表示已收藏
        ifSave: false,
        badge: 0,
      },
      // productList
      productList: [],
      // 常见问题
      issue: [],
      // 相关产品
      goodsList: [],
    };
  },
  created() {
    GetProductDetailAPI({
      id: this.$route.params.id,
    }).then((res) => {
      if (res.errno == 0) {
        let { gallery, info, attribute, productList, issue } = res.data;
        this.gallery = gallery;
        this.info = info;
        this.attribute = attribute;
        this.productList = productList;
        this.issue = issue;
        this.$refs.box.innerHTML = this.info.goods_desc;
      }
    });

    // 相关产品
    GetAboutAPI({
      id: this.$route.params.id,
    }).then((res) => {
      if (res.errno == 0) {
        let { goodsList } = res.data;
        this.goodsList = goodsList;
      }
    });

    // 获取购物车总数量
    GoodsCountAPI().then((res) => {
      if (res.errno == 0) {
        this.cartLanObj.badge = res.data.cartTotal.goodsCount;
      }
    });
  },
  components: {
    WanShan,
    ProductList,
  },
  // 监听路由变化
  watch: {
    $route: function(to, from){
      console.log(to.path, from.path)
      if(to.path !== from.path){
        // 当前页面重新加载
        window.location.reload();
      }
    }
  },
  methods: {
    // 跳去购物车
    goToCart() {
      this.$router.push("/cart");
    },
    // 点击了"加入购物车"
    addToCartFn() {
      // ifShowSku为true，此时的功能是添加到购物车
      // ifShowSku为false，此时显示sku
      if (this.ifShowSku) {
        AddToCartAPI({
          goodsId: this.$route.params.id,
          productId: this.productList[0].id,
          number: this.stepValue,
        }).then((res) => {
          if (res.errno == 0) {
            this.cartLanObj.badge = res.data.cartTotal.goodsCount;
          }
        });

        this.ifShowSku = false;
      } else {
        this.ifShowSku = true;
      }
    },

    // 立即购买
    buyNow() {
      this.$toast("该功能暂未开放");
    },
  },
};
</script>
 
<style lang = "less" scoped>
.product_info {
  background: #fff;
  line-height: 40px;
  text-align: center;
  padding: 20px 0;
  border-bottom: 1px solid #ccc;
  h3 {
    font-size: 20px;
  }
  div {
    color: #666;
  }
  p {
    color: darkred;
  }
}

.canshu {
  padding: 10px 4%;
  margin-top: 20px;
  background: #fff;
  h3 {
    height: 50px;
    line-height: 50px;
  }
  ul {
    li {
      background: #efefef;
      width: 100%;
      border-radius: 5px;
      height: 40px;
      line-height: 40px;
      margin-bottom: 10px;
      display: flex;
      justify-content: space-between;
      small {
        width: 22%;
        color: #666;
        text-align: right;
        margin-right: 10px;
      }
      big {
        flex: 1;
        padding-right: 10px;
      }
    }
  }
}

// 插入dom节点的元素无法修改样式
.box {
  /deep/img {
    width: 100%;
    display: block;
  }
}

.modal {
  position: fixed;
  z-index: 999;
  left: 0;
  top: 0;
  width: 100vw;
  // calc是css中用来计算不同单位的方法，减号前后要空格
  height: calc(100vh - 50px);
  .modal_bg {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
  }
  .modal_content {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    background: #fff;
    padding: 20px 4%;
    box-sizing: border-box;
    .top {
      display: flex;
      justify-content: space-between;
      .right {
        flex: 1;
        margin-left: 10px;
        line-height: 50px;
        /* display: flex;
                flex-direction: column;
                justify-content: space-around; */
      }
    }
    .close {
      position: absolute;
      right: 10px;
      top: 10px;
    }
    .down {
      h3 {
        height: 80px;
        line-height: 80px;
      }
    }
  }
}

.mytitle {
  background: #fff;
  height: 50px;
  line-height: 50px;
  position: relative;
  h3 {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 30%;
    background: #fff;
    text-align: center;
  }
  span {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    height: 2px;
    width: 50%;
    background: #ccc;
  }
}

.allLook{
  background: #fff;
}

.issue {
  background: #fff;
  ul {
    padding: 0 4%;
    li {
      h3 {
        position: relative;
        padding-left: 20px;
        margin-bottom: 10px;
        i {
          width: 4px;
          height: 4px;
          background: darkred;
          position: absolute;
          top: 50%;
          left: 0;
          margin-top: -2px;
        }
      }
      div {
        padding-left: 20px;
        color: #666;
        font-size: 12px;
        padding-bottom: 10px;
      }
    }
  }
}
</style>